<template>
  <div class="bind-phone">
    <!-- 顶部导航栏 -->
    <div class="nav-header">
      <van-icon class="back-icon" name="arrow-left" @click="goBack" />
      <span class="nav-title">绑定手机号</span>
    </div>

    <div class="content">
      <div class="form-card">
        <!-- 手机号输入区域 -->
        <div class="input-group">
          <div class="phone-input">
            <van-field
                v-model="phoneNumber"
                placeholder="请输入手机号"
                class="phone-field"
                maxlength="11"
                clearable
            >
              <template #label>
                <span class="area-code">+86</span>
              </template>
            </van-field>
            <van-button
                class="code-btn"
                :class="{ 'counting': isCounting }"
                @click="sendCode"
                :disabled="isCounting"
            >
              {{ isCounting ? countdown + '秒' : '获取验证码' }}
            </van-button>
          </div>
        </div>

        <!-- 验证码输入区域 -->
        <div class="input-group">
          <van-field
              v-model="verificationCode"
              placeholder="请输入验证码"
              class="code-field"
              maxlength="6"
              clearable
          >
            <template #label>
              <span class="field-label">验证码</span>
            </template>
          </van-field>
        </div>

        <!-- 提示信息 -->
        <div class="tips">
          <van-icon name="info-o" />
          <span>验证码将发送至您的手机，请注意查收</span>
        </div>

        <!-- 提交按钮 -->
        <div class="submit-btn">
          <van-button
              type="primary"
              block
              :disabled="!phoneNumber || !verificationCode"
              @click="submit"
          >
            确认绑定
          </van-button>
        </div>
      </div>
    </div>
  </div>
</template>

<script>
import axios from "axios";

export default {
  name: 'BindPhoneView',
  data() {
    return {
      id:"",
      phoneNumber: '',
      verificationCode: '',
      isCounting: false, // 是否在倒计时
      countdown: 60, // 倒计时初始值
      timer: null, // 定时器
      userphone:"",
    };
  },
  created() {
    this.user=localStorage.getItem("user");

    let user=JSON.parse(this.user);
    console.log(user.id)
    this.id=user.id;
  },
  methods: {
    goBack() {
      this.$router.go(-1); // 返回上一页
    },
    sendCode() {
      if (this.phoneNumber === '') {
        this.$toast.fail("请输入手机号");
        return;
      }
      if (!/1[3-9][0-9]{9}/g.test(this.phoneNumber)) {
        this.$toast.fail("请输入合法手机号");
        return;
      }

      // 发送验证码的逻辑
      axios.post(`http://localhost:9999/api-user/user/sendcode1?phone=` + this.phoneNumber).then(res => {
        if (res.data.code === 1) {
          this.$toast.success("验证码已发送");
          this.startCountdown(); // 启动倒计时
        } else {
          this.$toast.fail(res.data.message);
        }
      }).catch(error => {
        this.$toast.fail("请求失败，请刷新重试")
      });
    },
    startCountdown() {
      this.isCounting = true;
      this.countdown = 60; // 重置倒计时
      this.timer = setInterval(() => {
        if (this.countdown <= 0) {
          this.isCounting = false;
          clearInterval(this.timer);
        } else {
          this.countdown--;
        }
      }, 1000);
    },
    submit() {
      // 提交绑定手机号的逻辑
      axios.post(`http://localhost:9999/api-user/user/updatephone?phone=` + this.phoneNumber + `&code=` + this.verificationCode+"&id="+this.id).then(res => {
        if (res.data.code === 1) {
          this.$toast.success("绑定成功");
          // this.$router.replace('/userInfo');
          this.$router.push({path:'/userInfo',query:{id:this.id}});

        } else {
          this.$toast.fail(res.data.message);
        }
      }).catch(error => {
        this.$toast.fail("请求失败，请刷新重试")
      });
    }
  },
  beforeDestroy() {
    // 清理定时器
    if (this.timer) {
      clearInterval(this.timer);
    }
  }
}
</script>

<style scoped>
.bind-phone {
  background-color: #f5f5f5;
  min-height: 100vh;
}

.nav-header {
  position: fixed;
  top: 0;
  left: 0;
  right: 0;
  height: 44px;
  background: #fff;
  display: flex;
  align-items: center;
  padding: 0 16px;
  z-index: 100;
  box-shadow: 0 1px 2px rgba(0,0,0,0.05);
}

.back-icon {
  font-size: 20px;
  color: #333;
}

.nav-title {
  flex: 1;
  text-align: center;
  font-size: 16px;
  font-weight: 500;
  margin-right: 24px;
}

.content {
  padding: 60px 16px 16px;
}

.form-card {
  background: #fff;
  border-radius: 12px;
  padding: 20px;
  box-shadow: 0 2px 8px rgba(0,0,0,0.05);
}

.input-group {
  margin-bottom: 20px;
}

.phone-input {
  display: flex;
  align-items: center;
  gap: 12px;
}

.phone-field {
  flex: 1;
}

.area-code {
  color: #333;
  font-weight: 500;
  font-size: 15px;
  padding-right: 8px;
  border-right: 1px solid #eee;
}

.field-label {
  color: #333;
  font-weight: 500;
  font-size: 15px;
}

.code-btn {
  width: 110px;
  height: 36px;
  border-radius: 18px;
  font-size: 14px;
  background: #0073e6;
  color: #fff;
  border: none;
}

.code-btn.counting {
  background: #e8e8e8;
  color: #999;
}

.code-field {
  width: 100%;
}

.tips {
  display: flex;
  align-items: center;
  gap: 6px;
  color: #999;
  font-size: 13px;
  margin: 16px 0;
}

.tips .van-icon {
  color: #0073e6;
}

.submit-btn {
  margin-top: 32px;
}

.submit-btn .van-button {
  height: 44px;
  border-radius: 22px;
  font-size: 16px;
  background: #0073e6;
  border: none;
}

.submit-btn .van-button--disabled {
  background: #e8e8e8;
  color: #999;
}

/* 输入框样式优化 */
:deep(.van-field__control) {
  font-size: 15px;
  color: #333;
}

:deep(.van-field__control::placeholder) {
  color: #999;
}

:deep(.van-cell) {
  padding: 12px 0;
  background: transparent;
}
</style>
